home *** CD-ROM | disk | FTP | other *** search
/ Assassins - Ultimate CD Games Collection 4 / Assassins 4 (1999)(Weird Science).iso / mui / mui_developer / c / examples / class1.c < prev    next >
C/C++ Source or Header  |  1997-03-10  |  6KB  |  209 lines

  1. #include "demo.h"
  2.  
  3.  
  4. /***************************************************************************/
  5. /* Here is the beginning of our simple new class...                        */
  6. /***************************************************************************/
  7.  
  8. /*
  9. ** This is an example for the simplest possible MUI class. It's just some
  10. ** kind of custom image and supports only two methods: 
  11. ** MUIM_AskMinMax and MUIM_Draw.
  12. */
  13.  
  14. /*
  15. ** This is the instance data for our custom class.
  16. ** Since it's a very simple class, it contains just a dummy entry.
  17. */
  18.  
  19. struct MyData
  20. {
  21.     LONG dummy;
  22. };
  23.  
  24.  
  25. /*
  26. ** AskMinMax method will be called before the window is opened
  27. ** and before layout takes place. We need to tell MUI the
  28. ** minimum, maximum and default size of our object.
  29. */
  30.  
  31. SAVEDS ULONG mAskMinMax(struct IClass *cl,Object *obj,struct MUIP_AskMinMax *msg)
  32. {
  33.     /*
  34.     ** let our superclass first fill in what it thinks about sizes.
  35.     ** this will e.g. add the size of frame and inner spacing.
  36.     */
  37.  
  38.     DoSuperMethodA(cl,obj,msg);
  39.  
  40.     /*
  41.     ** now add the values specific to our object. note that we
  42.     ** indeed need to *add* these values, not just set them!
  43.     */
  44.  
  45.     msg->MinMaxInfo->MinWidth  += 100;
  46.     msg->MinMaxInfo->DefWidth  += 120;
  47.     msg->MinMaxInfo->MaxWidth  += 500;
  48.  
  49.     msg->MinMaxInfo->MinHeight += 40;
  50.     msg->MinMaxInfo->DefHeight += 90;
  51.     msg->MinMaxInfo->MaxHeight += 300;
  52.  
  53.     return(0);
  54. }
  55.  
  56.  
  57. /*
  58. ** Draw method is called whenever MUI feels we should render
  59. ** our object. This usually happens after layout is finished
  60. ** or when we need to refresh in a simplerefresh window.
  61. ** Note: You may only render within the rectangle
  62. **       _mleft(obj), _mtop(obj), _mwidth(obj), _mheight(obj).
  63. */
  64.  
  65. SAVEDS ULONG mDraw(struct IClass *cl,Object *obj,struct MUIP_Draw *msg)
  66. {
  67.     int i;
  68.  
  69.     /*
  70.     ** let our superclass draw itself first, area class would
  71.     ** e.g. draw the frame and clear the whole region. What
  72.     ** it does exactly depends on msg->flags.
  73.     */
  74.  
  75.     DoSuperMethodA(cl,obj,msg);
  76.  
  77.     /*
  78.     ** if MADF_DRAWOBJECT isn't set, we shouldn't draw anything.
  79.     ** MUI just wanted to update the frame or something like that.
  80.     */
  81.  
  82.     if (!(msg->flags & MADF_DRAWOBJECT))
  83.         return(0);
  84.  
  85.     /*
  86.     ** ok, everything ready to render...
  87.     */
  88.  
  89.     SetAPen(_rp(obj),_dri(obj)->dri_Pens[TEXTPEN]);
  90.  
  91.     for (i=_mleft(obj);i<=_mright(obj);i+=5)
  92.     {
  93.         Move(_rp(obj),_mleft(obj),_mbottom(obj));
  94.         Draw(_rp(obj),i,_mtop(obj));
  95.         Move(_rp(obj),_mright(obj),_mbottom(obj));
  96.         Draw(_rp(obj),i,_mtop(obj));
  97.     }
  98.  
  99.     return(0);
  100. }
  101.  
  102.  
  103. /*
  104. ** Here comes the dispatcher for our custom class. We only need to
  105. ** care about MUIM_AskMinMax and MUIM_Draw in this simple case.
  106. ** Unknown/unused methods are passed to the superclass immediately.
  107. */
  108.  
  109. SAVEDS ASM ULONG MyDispatcher(REG(a0) struct IClass *cl,REG(a2) Object *obj,REG(a1) Msg msg)
  110. {
  111.     switch (msg->MethodID)
  112.     {
  113.         case MUIM_AskMinMax: return(mAskMinMax(cl,obj,(APTR)msg));
  114.         case MUIM_Draw     : return(mDraw     (cl,obj,(APTR)msg));
  115.     }
  116.  
  117.     return(DoSuperMethodA(cl,obj,msg));
  118. }
  119.  
  120.  
  121.  
  122. /***************************************************************************/
  123. /* Thats all there is about it. Now lets see how things are used...        */
  124. /***************************************************************************/
  125.  
  126. int main(int argc,char *argv[])
  127. {
  128.     APTR app,window,MyObj;
  129.     struct MUI_CustomClass *mcc;
  130.  
  131.     init();
  132.  
  133.     /* Create the new custom class with a call to MUI_CreateCustomClass(). */
  134.     /* Caution: This function returns not a struct IClass, but a           */
  135.     /* struct MUI_CustomClass which contains a struct IClass to be         */
  136.     /* used with NewObject() calls.                                        */
  137.     /* Note well: MUI creates the dispatcher hook for you, you may         */
  138.     /* *not* use its h_Data field! If you need custom data, use the        */
  139.     /* cl_UserData of the IClass structure!                                */
  140.  
  141.     if (!(mcc = MUI_CreateCustomClass(NULL,MUIC_Area,NULL,sizeof(struct MyData),MyDispatcher)))
  142.         fail(NULL,"Could not create custom class.");
  143.  
  144.     app = ApplicationObject,
  145.         MUIA_Application_Title      , "Class1",
  146.         MUIA_Application_Version    , "$VER: Class1 19.5 (12.02.97)",
  147.         MUIA_Application_Copyright  , "©1993, Stefan Stuntz",
  148.         MUIA_Application_Author     , "Stefan Stuntz",
  149.         MUIA_Application_Description, "Demonstrate the use of custom classes.",
  150.         MUIA_Application_Base       , "CLASS1",
  151.  
  152.         SubWindow, window = WindowObject,
  153.             MUIA_Window_Title, "A Simple Custom Class",
  154.             MUIA_Window_ID   , MAKE_ID('C','L','S','1'),
  155.             WindowContents, VGroup,
  156.  
  157.                 Child, MyObj = NewObject(mcc->mcc_Class,NULL,
  158.                     TextFrame,
  159.                     MUIA_Background, MUII_BACKGROUND,
  160.                     TAG_DONE),
  161.  
  162.                 End,
  163.  
  164.             End,
  165.         End;
  166.  
  167.     if (!app)
  168.         fail(app,"Failed to create Application.");
  169.  
  170.     DoMethod(window,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,
  171.         app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);
  172.  
  173.  
  174.  
  175. /*
  176. ** This is the ideal input loop for an object oriented MUI application.
  177. ** Everything is encapsulated in classes, no return ids need to be used,
  178. ** we just check if the program shall terminate.
  179. ** Note that MUIM_Application_NewInput expects sigs to contain the result
  180. ** from Wait() (or 0). This makes the input loop significantly faster.
  181. */
  182.  
  183.     set(window,MUIA_Window_Open,TRUE);
  184.  
  185.     {
  186.         ULONG sigs = 0;
  187.  
  188.         while (DoMethod(app,MUIM_Application_NewInput,&sigs) != MUIV_Application_ReturnID_Quit)
  189.         {
  190.             if (sigs)
  191.             {
  192.                 sigs = Wait(sigs | SIGBREAKF_CTRL_C);
  193.                 if (sigs & SIGBREAKF_CTRL_C) break;
  194.             }
  195.         }
  196.     }
  197.  
  198.     set(window,MUIA_Window_Open,FALSE);
  199.  
  200.  
  201. /*
  202. ** Shut down...
  203. */
  204.  
  205.     MUI_DisposeObject(app);     /* dispose all objects. */
  206.     MUI_DeleteCustomClass(mcc); /* delete the custom class. */
  207.     fail(NULL,NULL);            /* exit, app is already disposed. */
  208. }
  209.